from wechatpayv3 import WeChatPay, WeChatPayType

# 微信支付商户号
MCHID = '1614068995'
# 商户证书私钥
PRIVATE_KEY_PATH = './edoego/apiclient_key.pem'
with open(PRIVATE_KEY_PATH, 'r') as f:
    PRIVATE_KEY = f.read()
# 商户证书序列号
CERT_SERIAL_NO = '27D1EEBF153563466F4AD44A9E1F0E3A7E77F817'
# API v3密钥
APIV3_KEY = 'EdoegoEdoegoEdoego2024Edoego2024'
# APPID
APPID = 'wxe8411de4faf8fb57'
# 回调地址
NOTIFY_URL = 'https://api.eutong.top/api/wx_pay'
# 微信支付平台证书缓存目录
CERT_DIR = './cert'

# 初始化WeChatPay对象
wxpay = WeChatPay(
    wechatpay_type=WeChatPayType.JSAPI,  # 根据你的业务选择合适的支付类型
    mchid=MCHID,
    private_key=PRIVATE_KEY,
    cert_serial_no=CERT_SERIAL_NO,
    apiv3_key=APIV3_KEY,
    appid=APPID,
    notify_url=NOTIFY_URL,
    cert_dir=CERT_DIR
)

# 转账给用户
def transfer_to_user(wxpay, amount, partner_trade_no, openid,phone):
    # 转账参数
    detail_list = [{'out_detail_no':partner_trade_no,"transfer_amount":amount,"transfer_remark":"转账给用户:"+phone,
                    "openid": openid, "user_name": phone}]
    # 发起转账请求
    response = wxpay.transfer_batch(partner_trade_no,"转账给用户:"+phone,"转账给用户:"+phone,
                                    amount,1,detail_list)
    return response

# 使用示例
# amount = 300  # 转账金额，单位为分
# partner_trade_no = '0100000'  # 需要生成一个唯一的商户订单号
# openid = 'o5XO14kDtMor_BuDzaR60FWl4-2I'  # 需要获取用户的openid
# response = transfer_to_user(wxpay, amount, partner_trade_no, openid)
# print(response)